package com.woniuxy.house.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.entity.house.Secondhouse;
import com.woniuxy.entity.house.dto.SecondHouseTypeDto;
import com.woniuxy.entity.house.dto.SecondhouseDto;
import com.woniuxy.entity.house.dto.SecondhouseQO;
import com.woniuxy.house.feign.FileService;
import com.woniuxy.house.service.SecondhouseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/house/secondhouse")
public class SecondhouseController {

    @Resource
    private SecondhouseService secondhouseService;

    @Resource
    private FileService fileService;

    /**
     * 新增二手房信息
     */
    @PostMapping("/add")
    public ResponseResult<Void> add(Secondhouse secondhouse, MultipartFile file){
        secondhouse.setImage(fileService.upload(file).getData());
        return secondhouseService.save(secondhouse) ?
                new ResponseResult<>(500,"新增二手房成功！") :
                new ResponseResult<>(200,"新增二手房失败！");
    }

    /**
     * 分页查询所有二手房信息
     */
    @GetMapping("/page/{current}/{size}")
    public ResponseResult<Page<Secondhouse>> page(@PathVariable int current, @PathVariable int size){
        return new ResponseResult<>(secondhouseService.findPage(current, size));
    }

    /**
     * 根据二手房id查询单个二手房及其户型，楼盘，评论信息
     */
    @GetMapping("/{id}")
    public ResponseResult<SecondhouseDto> findOneById(@PathVariable("id")int id){
        SecondhouseDto secondhouseDto = secondhouseService.findOneById(id);
        return secondhouseDto != null ?
                new ResponseResult<>(secondhouseDto) : new ResponseResult<>(500,"没有数据");
    }

    /**
     * 根据二手房id软删除租房信息，将status修改为1
     */
    @PostMapping("/{id}")
    public ResponseResult<Void> deleteOneById(@PathVariable int id) {
        return secondhouseService.deleteOneById(id) ? new ResponseResult<>(200,"删除成功！")
                : new ResponseResult<>(500,"删除失败！");
    }

    /**
     * 根据二手房id修改单个二手房信息
     */
    @PostMapping("/update")
    public ResponseResult<Void> update(@RequestBody Secondhouse secondhouse) {
        return secondhouseService.updateBySid(secondhouse) ? new ResponseResult<>(200,"修改成功！")
                : new ResponseResult<>(500,"修改失败！");
    }

    /**
     * 根据条件分页查询（包括排序）
     */
    @GetMapping("/findLike/{current}/{size}")
    public ResponseResult<Page<SecondHouseTypeDto>> findLike(@RequestBody SecondhouseQO secondhouseQO, @PathVariable int current, @PathVariable int size) {
        Page<SecondHouseTypeDto> pageLike = secondhouseService.findLike(current,size,secondhouseQO);
        return pageLike != null ?
                new ResponseResult<>(pageLike) :
                new ResponseResult<>(500,"未查询到数据");
    }

}

